package fhp.hlhj.giantfold.adapter
import android.app.Activity
import android.content.Intent
import android.widget.TextView
import com.alibaba.baichuan.android.trade.AlibcTrade
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback
import com.alibaba.baichuan.android.trade.model.AlibcShowParams
import com.alibaba.baichuan.android.trade.model.OpenType
import com.alibaba.baichuan.android.trade.page.AlibcPage
import com.alibaba.baichuan.trade.biz.context.AlibcTradeResult
import com.alibaba.baichuan.trade.biz.core.taoke.AlibcTaokeParams
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.activity.malls.TBOrderListAty
import fhp.hlhj.giantfold.javaBean.SuperMarketListBean
import fhp.hlhj.giantfold.network.Urls
import fhp.hlhj.giantfold.utils.LogUtil
/**
 * author: FHP
 * data: on 2017\11\27 0027 16:33
 * E-Mail: 103945089@qq.com
 */
class SuperMarketRcyAdp(layoutId:Int, data:List<SuperMarketListBean.DataBean>,private val activity:Activity) :BaseQuickAdapter<SuperMarketListBean.DataBean,BaseViewHolder>(layoutId,data) {
    override fun convert(helper: BaseViewHolder, item: SuperMarketListBean.DataBean?) {
        helper.setText(R.id.itemTitle,item?.goods_name)
        helper.setText(R.id.itemPrice,item?.price_after_coupons)
        helper.setText(R.id.itemOldPrice,item?.price)
        helper.setText(R.id.itemCut,"领券减"+item?.price_coupons)
        if (item?.pic?.substring(0,2)!!.equals("//")){
            item?.pic="http:"+item?.pic
        }
        Glide.with(mContext).load(item?.pic).asBitmap().dontAnimate().into(helper.getView(R.id.itemIcon))

        val btPon = helper.getView<TextView>(R.id.btPon)
        btPon.setOnClickListener {
            val quanStr = item?.quan_link
            val activityId= quanStr.split("activity_id=")[1]
            val page = AlibcPage(Urls.TK_QUAN_URL+"activityId=$activityId&itemId=${item?.goods_id}")
            val showParams = AlibcShowParams(OpenType.H5, false)
            val params = AlibcTaokeParams()
            params.pid="mm_116147303_36832537_132678582"
            AlibcTrade.show(activity,page,showParams,params,null,object :AlibcTradeCallback{
                override fun onFailure(p0: Int, p1: String?) {
                    LogUtil.logi("错误$p1")
                }

                override fun onTradeSuccess(p0: AlibcTradeResult?) {
                    LogUtil.logi("成功")
                    val intent = Intent(mContext, TBOrderListAty::class.java)
                    intent.putExtra("pid",item.goods_id)
                    intent.putExtra("et",5)
                    intent.putExtra("rate",item.rate)
                    mContext.startActivity(intent)
                }
            })
        }
    }

}